package com.hemerasolutions.v2trainer.model.exercise;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import com.hemerasolutions.v2trainer.model.base.BaseValueHolder;
import com.hemerasolutions.v2trainer.model.exercise.executions.Execution;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityExecution;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityModZone;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityTable;
import com.hemerasolutions.v2trainer.model.exercise.intensity.IntensityZone;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.step.Step;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.model.unit.volume.VolumeType;
import com.hemerasolutions.v2trainer.util.Utility;

@Entity
public class TemplateExercise extends BaseValueHolder {

	private static final long serialVersionUID = 1L;

    @Column
	private char variation;

    @ManyToOne
    private TrainingType trainingType;
    
    @ManyToOne
    private Modality modality;
    
    @ManyToOne
    private Execution execution;
    
    @ManyToOne
	private IntensityModZone intensityModZone;

    @ManyToOne
    private VolumeType volumeType;
    
    @OneToOne
    private Step step;
    
    @Column
	private int physicCap;
    
    
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public TemplateExercise(){
		super();
	}
	
	public TemplateExercise(Step step) {
		super();
		this.step = step;
	}

	public TemplateExercise(TemplateExercise other, Step parent) {
		this();
		this.modality = other.modality;
		this.trainingType = other.trainingType;
		this.variation = other.variation;
		this.execution = other.execution;
		this.intensityModZone = other.intensityModZone;
		this.physicCap = other.physicCap;
		this.step = parent;
		this.value = other.value;
		this.volumeFactor = other.volumeFactor;
		this.volumeType = other.volumeType;
	}

	
	public TemplateExercise(TemplateExercise other) {
		this();
		this.modality = other.modality;
		this.trainingType = other.trainingType;
		this.variation = other.variation;
		this.execution = other.execution;
		this.intensityModZone = other.intensityModZone;
		this.physicCap = other.physicCap;
		this.value = other.value;
		this.volumeFactor = other.volumeFactor;
		this.volumeType = other.volumeType;
	}	

	public String getTranslatedExercise(){
		String desc = "";
		String restTime = Utility.timeToString(this.step.getRestInterval());

		desc = this.step.getRepetition() > 1 ? 	" " + this.step.getRepetition() +"x ": " ";

		try{
			
			if(this.getVolumeFactor().isTimeBased()){
				desc += this.getIntervalToString() + " " + Utility.getText("Exercise")  + " " + this.variation ;			
			}else{
				desc += this.getStringValue() + " " + Utility.getText(this.getVolumeFactor().getName()) + " " + Utility.getText("Exercise")  + " " + this.variation;					
			}

			if(this.intensityModZone != null){
				desc = desc	+ " " +	 this.intensityModZone.getName();
			}
			
			if(!restTime.equals("")){
				desc += " " + "ID: " + restTime;
			}		
		
		}catch (Exception e) {
			
		}
		return desc;		
		
	}
	
	public String getStringValue(){
		return this.getVolumeFactor().format(this.getValue());
	}
		
	public IntensityZone getIntensityZone(Exercise exercise) {
	
		IntensityZone izReturn = null;
		
		for(IntensityExecution ie: exercise.getIntensityExecutions()){
			if(ie.getExecution().equals(this.intensityModZone.getIntensityModTable().getExecution())){
				for(IntensityTable it: ie.getIntensityTables()){
					if(it.getLevel() == this.getStep().getTrainingStep().getLevel()){
						for(IntensityZone iz:it.getIntensitys()){
							if(iz.getName().equals(intensityModZone.getName())){
								return iz;
							}
						}
						if(izReturn == null){
							for(IntensityZone iz:it.getIntensitys()){
								if(iz.getPosition() == intensityModZone.getPosition()){
									return iz;
								}
							}
						}
					}
				}
			}
		}
		
		return izReturn;
	}	
	
	
	public char getVariation() {
		return variation;
	}

	public Modality getModality() {
		return modality;
	}

	public IntensityModZone getIntensityModZone() {
		return intensityModZone;
	}

	public void setVariation(char variation) {
		this.variation = variation;
	}

	public void setModality(Modality modality) {
		this.modality = modality;
	}

	public void setIntensityModZone(IntensityModZone intensityModZone) {
		this.intensityModZone = intensityModZone;
	}

	public TrainingType getTrainingType() {
		return trainingType;
	}


	public void setTrainingType(TrainingType trainingType) {
		this.trainingType = trainingType;
	}


	public Execution getExecution() {
		return execution;
	}


	public void setExecution(Execution execution) {
		this.execution = execution;
	}


	public VolumeType getVolumeType() {
		return volumeType;
	}


	public void setVolumeType(VolumeType volumeType) {
		this.volumeType = volumeType;
	}


	public Step getStep() {
		return step;
	}


	public void setStep(Step step) {
		this.step = step;
	}


	public int getPhysicCap() {
		return physicCap;
	}


	public void setPhysicCap(int physicCap) {
		this.physicCap = physicCap;
	}

	public String getIntervalToString(){
		return Utility.timeToString(this.getDateValue());
	}
		

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((step == null) ? 0 : step.hashCode());
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TemplateExercise other = (TemplateExercise) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (step == null) {
			if (other.step != null)
				return false;
		} else if (!step.equals(other.step))
			return false;
		if (uuid == null) {
			if (other.uuid != null)
				return false;
		} else if (!uuid.equals(other.uuid))
			return false;
		return true;
	}


	@Override
	public String toString() {
		return "TemplateExercise [id=" + id + ", step=" + step + ", uuid="
				+ uuid + "]";
	}

}
